<?php
class MainAction extends Action {
	public function index(){
		isLogin();
		$this->display();
	}
	public function collect(){
		if (isset($_POST['submit'])) {
			$Pregs = D('Pregs');
			$validate=array(
				array('website','require','invalid website'),
				array('dir','require','invalid dir'),
				array('url','url','invalid url'),
				array('pregs','require','invalid pregs'),
			);
			$Pregs->setProperty("_validate",$validate); 
			$s = $Pregs->create();
			if (!$s) {
				showAlert($Pregs->getError());
			} else {
				$Pregs->add();
				showMsg('添加成功',"index.php?s=/Supadmin/main/collect_list");
			}
		}
		$this->display();
	}
	public function collect_list(){
		$startpage = isset($_GET['page'])?$_GET['page']:1;
		$per = 2;
		$start = ($startpage - 1) * $per > 0 ? ($startpage - 1) * $per : 0;
		if($per > 0)
		{
		    $LIMIT = ' LIMIT '.$start.','.$per;
		}
		$sql = "select * from think_pregs where 1 Order by dir {$LIMIT} ";
		$sqlc = 'select count(*) as amount from think_pregs';
		$Model = new Model(); // 实例化一个 model 对象 没有对应任何数据表 
		$pregs_list = $Model->query($sql); 
		$c = $Model->query($sqlc);
		$amount = $c[0]['amount'];
		$pagestr = pageStr($amount,$startpage,$per, 'index.php?s=/Supadmin/Main/collect_list/');
		$this->assign('pregs_list', $pregs_list);
		$this->assign('pagestr',$pagestr);
		$this->display();
	}
	public function upload(){
		isLogin();
		$dir = './Public/uploads/';
		$optf = getDir($dir);
		$dir .= $optf[0];
		$opts = getDir($dir);
	    $this->assign("optf", $optf);
	    $this->assign("opts", $opts);
		$this->display();
	}
	public function flushcache(){
		$names_cache_file = 'Public/cache/names_cache.php';
		if (is_file($names_cache_file)) {
			@unlink($names_cache_file);
			showMsg('Flush names cache successfully!','index.php?s=/Supadmin/Main/cache');
		}else {
			showAlert('No Names Cache is set!');
		}
	}
	public function cache(){
		$cover_pics = getPicDir2();
		$this->assign('cover_pics', $cover_pics);
		$this->display();
	}
	public function help(){
		$this->display();
	}
	public function upimgs(){
		if (isset($_POST["submit"])) {
			$picpath = 'Public/uploads/'.$_POST['fnames'].'/'.$_POST['snames'].'/';
			$pic_files = &$_FILES['pics']; 
			$amount = count($pic_files['name']);
			$flexx = array("image/gif"=>"gif", "image/jpeg"=>'jpg', "image/pjpeg"=>'jpg',   "image/png"=>'png', "image/x-png"=>'png');
			$counter = 0;
			for ($i=0; $i<$amount; $i++){
				if ($flexx[$pic_files['size'][$i]] > 1048576) {
					continue;
				}
				$counter++;
				$newfile = time().rand(1,9999);
				$flex = isset($flexx[$pic_files['type'][$i]])?$flexx[$pic_files['type'][$i]]:'jpg';
				$filename = $picpath . $newfile . '.'.$flex;
				$filename1 = $picpath . '00_' . $newfile . '.'.$flex;
				if ($i==0) {
					imageIsResize($pic_files["tmp_name"][$i], 160, 180, $filename1);
				}
				imageResize($pic_files["tmp_name"][$i], 720, 540, $filename);
				unset($_FILES[$i]);
			} 
		}
		showAlert('Upload successfully! total:'.$amount.'->success: '.$counter);
	}
	public function ajax(){
		$oper = isset($_GET['oper'])?$_GET['oper']:'';
		if ($oper == 'mkdir') {
			$dir = $_GET['dir'];
			$fdir = $_GET['fdir'];
			$updir = 'Public/uploads/'.$fdir.'/'.$dir;
			if (!is_dir($updir)) {
				@mk_dir($updir,0777);
				die($dir);
			}else {
				die('1');
			} 
		}else if ($oper == 'chgdir') {
			$fdir = $_GET['fdir']; 
			$updir = 'Public/uploads/'.$fdir;
			$sdirs = getDir($updir);
			die($sdirs);
		}
	}
	public function adsindex(){
		$adsindex_cache_file = 'Public/cache/adsindex_cache.php';
		if (isset($_POST['submit'])) {
			$position = $_POST['position'];
			$ads = $_POST['ads'];
			$ads_arr = filterAdsTxt($ads, $position);
			file_put_contents($adsindex_cache_file, json_encode($ads_arr));
		}
		$ads_arr = array();
		if (is_file($adsindex_cache_file)) {
			$ads_json = file_get_contents($adsindex_cache_file);
			$ads_arr = json_decode($ads_json); 
			$ads_arr = array($ads_arr);
			$this->assign('ads_arr', $ads_arr[0]);
		}
		$this->display();
	}
	public function adsbeautyshow(){
		
		$this->display();
	}
	public function adstaobaomodel(){
		
		$this->display();
	}
}
?>